package com.example.administrator.student4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.student4.Adapter.SearchByClassAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * Created by 周千遇 on 2016/11/19.
 */
public class SearchByClass extends Activity {
    TextView tv_class;
    JSONArray array;
    String class_a,subject;
    ListView lv;
    JSONObject json;
    Myhandler myhandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchbyclass);
        myhandler = new Myhandler();
        tv_class = (TextView) findViewById(R.id.class_a);
        Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            class_a = bundle.getString("class_a");
            subject = bundle.getString("subject");
            json = new JSONObject();
            try {
                json.put("class_a",class_a);
                json.put("subject",subject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        new Thread(){
            @Override
            public void run() {
                super.run();
                String path = "http://10.0.2.2:8080/andorid/grade?request=findbyclassandsubject";
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    WebUtil.jsonOut(json,connection);
                    array = WebUtil.ioToJsonArray(connection.getInputStream());
                    myhandler.sendEmptyMessage(0x111);
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        lv = (ListView) findViewById(R.id.listView1);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchByClass.this,ModifyActivity.class);
                try {
                    intent.putExtra("name",array.getJSONObject(position).getString("name"));
                    intent.putExtra("subject",array.getJSONObject(position).getString("subject"));
                    intent.putExtra("term",array.getJSONObject(position).getString("term"));
                    intent.putExtra("grade", array.getJSONObject(position).getString("grade"));
                    intent.putExtra("id", array.getJSONObject(position).getInt("id"));
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    class Myhandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0x111){
                SearchByClassAdapter adapter = new SearchByClassAdapter(SearchByClass.this,array);
                lv.setAdapter(adapter);
            }
        }
    }
}
